﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;


public partial class ItemPage : System.Web.UI.Page
{
    private static BookInfo book;
    private Book bookBll = new Book();
    private Cart cart = new Cart();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            book = bookBll.GetBook(Request.QueryString["ISBN"]);
            ISBNLabel.Text = book.ISBN;
            BookPrice.Text = book.BookPrice.ToString();
            IntroLabel.Text = book.BookDesc;
            ShelfLabel.Text = book.BookShelfTime.ToShortDateString();
            DateLabel.Text = book.BookReleaseTime.ToShortDateString();
            BookStoreLabel.Text = book.BookRemainNum.ToString();
            BookSoldLabel.Text = book.BookSoldNum.ToString();
            BookPicture.ImageUrl = book.BookPicture;
            AuthorLabel.Text = book.BookAuthor;
            if (book.BookRemainNum == 0)
            {
                CartButton.Enabled = false;
                return;
            }
            CartButton.Enabled = true;
            List<Object> buyNums = new List<Object>();
            int max;
            max = book.BookRemainNum < 100 ? book.BookRemainNum : 99;
            for (int i = 1; i <= max; i++)
            {
                buyNums.Add(i);
            }
            BuyNum.DataSource = buyNums;
            BuyNum.DataBind();
        }
    }
    protected void CartButton_Click(object sender, EventArgs e)
    {
        if (BuyNum.SelectedIndex == -1)
        {
            Response.Write("<script>alert('请选择购买数量')</script>");
            return;
        }
        try
        {

            Profile.ShoppingCart.Add(book.ISBN, BuyNum.SelectedIndex + 1);
            Profile.Save();
            Response.Write("<script>alert('成功加入购物车')</script>");
        }
        catch(Exception ex)
        {
            Response.Write("<script>alert('加入购物车失败:" +  ex.ToString() + "')</script>");            
        }
    }
}